package cn.kgc.xinwen.service;

import cn.kgc.xinwen.mapper.DetailMapper;
import cn.kgc.xinwen.pojo.Detail;
import cn.kgc.xinwen.pojo.DetailExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-29 8:50
 */
@Service
public class DetailServiceImpl implements DetailService{

    @Resource
    DetailMapper detailMapper;
    @Override
    public PageInfo<Detail> pageInfo(Integer pageNum, Integer pageSize, Integer categoryId, String title) {
        PageHelper.startPage(pageNum,pageSize);
        DetailExample example=new DetailExample();
        DetailExample.Criteria criteria = example.createCriteria();
        DetailExample.Criteria criteria1 = example.createCriteria();

        if(categoryId==0||title==null){
            List<Detail> details = detailMapper.selectByExample(null);
            PageInfo<Detail> pageInfo=new PageInfo<>(details);
            return pageInfo;
        }else if(categoryId!=0&&title==null){
            criteria.andCategoryidEqualTo(categoryId);
            List<Detail> details = detailMapper.selectByExample(example);
            PageInfo<Detail> pageInfo=new PageInfo<>(details);
            return pageInfo;
        }else if(categoryId==0&&title!=null){
            criteria1.andTitleEqualTo(title);
            List<Detail> details = detailMapper.selectByExample(example);
            PageInfo<Detail> pageInfo=new PageInfo<>(details);
            return pageInfo;
        }else{
            criteria.andCategoryidEqualTo(categoryId);
            criteria1.andTitleEqualTo(title);
            example.or(criteria1);
            List<Detail> details = detailMapper.selectByExample(example);
            PageInfo<Detail> pageInfo=new PageInfo<>(details);
            return pageInfo;
        }
    }

    @Override
    public Detail selectById(Integer id) {
        return detailMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer update(Detail detail) {
        return detailMapper.updateByPrimaryKeySelective(detail);
    }
}
